<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>{{ menuTitle.main }}</el-breadcrumb-item>
      <el-breadcrumb-item>{{ menuTitle.item }}</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card class="box-card">
      <el-alert title="商品运营数据折线图" type="warning" center show-icon :closable="false"> </el-alert>
      <div id="main" style="height:600px;"></div>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 面包屑导航标题
      menuTitle: {
        main: '',
        item: ''
      },
      // 需要合并的数据
      options: {
        title: { text: '' },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross',
            label: { backgroundColor: '#E9EEF3' }
          }
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: { boundaryGap: false, type: 'category', data: [] },
        yAxis: { type: 'value' },
        series: [
          {
            name: '商品数量',
            type: 'line',
            data: []
          }
        ]
      }
    }
  },
  mounted() {
    this.getMenuList()
    this.getReportValue()
  },
  methods: {
    // 获取面包屑导航标题
    async getMenuList() {
      const { data: res } = await this.$axios.get('/menu/getMenuList.do')
      if (!res.flag) return this.$message.error(res.message)
      // 获取当前激活路径
      const activePath = window.sessionStorage.getItem('activePath')
      // 循环判断激活路径对应的路径名称
      res.data.forEach(value => {
        value.menuItem.forEach(item => {
          if (item.path === activePath) {
            this.menuTitle.item = item.title
            this.menuTitle.main = value.title
          }
        })
      })
    },
    // 获取图表数据
    async getReportValue() {
      // 基于准备好的 dom, 初始化 echarts 实例
      var myChart = this.$echarts.init(document.getElementById('main'))

      const { data: res } = await this.$axios.get('/reports/getReport.do')
      if (!res.flag) return this.$message.error(res.message)
      this.options.xAxis.data = res.data.months
      this.options.series[0].data = res.data.goodsCount

      myChart.setOption(this.options)
    }
  }
}
</script>

<style lang="less" scoped>
#main {
  background-color: #a6adb0;
}

.el-alert {
  margin-bottom: 10px;
}
</style>
